﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Basmus.Values.Attributive
{
    public class SetEqualConditionBuilder : IConditionBuilder
    {
        private readonly IPropertyInfoBuilder _propertyInfoBuilder;

        public SetEqualConditionBuilder(IPropertyInfoBuilder propertyInfoBuilder)
        {
            _propertyInfoBuilder = propertyInfoBuilder;
        }

        public IEnumerable<ICondition> Build(ValuesContext valuesContext)
        {
            var conditions = _propertyInfoBuilder
                .Build(valuesContext)
                .GetCustomAttributes(typeof (SetEqualConditionAttribute), false)
                .Cast<SetEqualConditionAttribute>()
                .Where(x => x.Key == valuesContext.ModelContext.SchemeKey)
                .Select(x => new SetEqualCondition(x.SetEqualType, x.Result, x.Values))
                .ToArray();
            return conditions;
        }
    }
}